package com.probox.modules.controller.customer;

import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.customer.CustomerInfo;
import com.probox.common.entity.domain.customer.CustomerLabelInfo;
import com.probox.common.entity.service.customer.*;
import com.probox.common.entity.service.order.IOrderRechargeService;
import com.probox.common.entity.vo.StaticQueryVo;
import com.probox.common.entity.vo.customer.*;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.customer.api.domain.CustomerInfoAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 客户信息Controller
 *
 * @author zsy
 * @date 2021-05-25
 */
@RestController
@RequestMapping("/customer/Info")
@Api(value = "客户模块", tags = {"客户信息后台操作接口"})
public class CustomerInfoController extends BaseController {
    @Autowired
    private ICustomerInfoService customerInfoService;

    @Autowired
    private ICustomerLevelInfoService customerLevelInfoService;

    @Autowired
    private ICustomerFundService customerFundService;

    @Autowired
    private ICustomerFundStoreService customerFundStoreService;

    @Autowired
    private IOrderRechargeService orderRechargeService;


    @Autowired
    private ICustomerLabelInfoService customerLabelInfoService;




    /**
     * 用户行为查询
     *
     * @return
     */
    @GetMapping("/getBehavior")
    public AjaxResult getBehavior(StaticQueryVo vo) {
        return AjaxResult.success(customerInfoService.getBehavior(vo));
    }

    /**
     * 查询客户信息列表
     */
    @ApiOperation(value = "获取客户列表")
    @PreAuthorize(hasPermi = "customer:Info:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerInfoVo customerInfo) {
        startPage();
        List<CustomerInfoLevelVo> list = customerInfoService.queryList(customerInfo);
        return getDataTable(list);
    }


    @GetMapping("/selectCustomer")
    public AjaxResult selectCustomer(CustomerInfoVo customerInfo) {
        return AjaxResult.success(customerInfoService.queryList(customerInfo));
    }


    /**
     * s
     * 查询本门店客户信息列表
     */
    @ApiOperation(value = "获取客户列表")
    @GetMapping("/listSelf")
    public TableDataInfo listSelf(CustomerInfoVo customerInfo) {
        customerInfo.setPlatformUid(SecurityUtils.getPlatformId());
        customerInfo.setStoreId(SecurityUtils.getStoreId());
        startPage();
        List<CustomerInfoLevelVo> list = customerInfoService.queryListSelf(customerInfo);
        return getDataTable(list);
    }


    /**
     * 导出客户信息列表
     */
    @ApiOperation(value = "导出客户信息列表")
    @PreAuthorize(hasPermi = "customer:Info:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerInfoVo customerInfo) throws IOException {
        List<CustomerInfoLevelVo> list = customerInfoService.queryList(customerInfo);
        ExcelUtil<CustomerInfoLevelVo> util = new ExcelUtil<CustomerInfoLevelVo>(CustomerInfoLevelVo.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @ApiOperation(value = "获取客户信息详细信息")
    @PreAuthorize(hasPermi = "customer:Info:query")
    @GetMapping(value = "/{customerUid}")
    public AjaxResult getInfo(@PathVariable("customerUid") Long customerUid) {
        return AjaxResult.success(customerInfoService.selectCustomerInfoById(customerUid));
    }

    /**
     * 新增客户信息
     */
    @ApiOperation(value = "新增客户信息")
    @PreAuthorize(hasPermi = "customer:Info:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerInfoVo customerInfoVo) {

        return toAjax(customerInfoService.insertCustomerInfo(customerInfoVo));
    }

    /**
     * 修改客户信息
     */
    @ApiOperation(value = "修改客户信息")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerInfoVo customerInfoVo) {

        return toAjax(customerInfoService.updateCustomerInfo(customerInfoVo));
    }

    /**
     * 删除客户信息
     */
    @ApiOperation(value = "删除客户信息")
    @PreAuthorize(hasPermi = "customer:Info:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{customerUids}")
    public AjaxResult remove(@PathVariable Long[] customerUids) {
        return toAjax(customerInfoService.deleteCustomerInfoByIds(customerUids));
    }

    /**
     * 修改客户状态信息
     */
    @ApiOperation(value = "修改客户状态信息")
    @PreAuthorize(hasPermi = "customer:Info:edit")
    @Log(title = "客户信息状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCustomerStatus")
    public AjaxResult updateCustomerStatus(@RequestBody CustomerInfoVo customerInfoVo) {
        return toAjax(customerInfoService.updateCustomerInfo(customerInfoVo));
    }

    /**
     * 导入下载模板
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "导入下载模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        util.importTemplateExcel(response, "客户数据");
    }

    @ApiOperation(value = "导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入的文件", paramType = "MultipartFile", required = true),
            @ApiImplicitParam(name = "password", value = "账号密码", paramType = "String", required = true),
    })
    @Log(title = "客户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize(hasPermi = "customer:Info:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String password) throws Exception {
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo>(CustomerInfo.class);
        List<CustomerInfo> customerInfoList = util.importExcel(file.getInputStream());
        String message = customerInfoService.importUser(customerInfoList, password);
        return AjaxResult.success(message);
    }

    /**
     * 根据id集合获取用户信息
     */
    @ApiOperation(value = "根据id集合获取用户信息")
    @PreAuthorize(hasPermi = "customer:Info:query")
    @GetMapping("/getCustomerInfoByids/{customerUids}")
    public TableDataInfo getCustomerInfoByids(@PathVariable Long[] customerUids) {
        startPage();
        List<CustomerInfoVo> list = customerInfoService.getCustomerInfoByids(customerUids);
        return getDataTable(list);
    }

    /**
     * 查看客户等级绑定记录
     */
    @ApiOperation(value = "查看客户等级绑定记录")
    @PreAuthorize(hasPermi = "customer:Info:query")
    @GetMapping("/getLevelInfoHistory/{customerUid}")
    public TableDataInfo getLevelInfoHistory(@PathVariable Long customerUid) {
        startPage();
        List<CustomerLevelInfoVo> levelInfoVoList = customerLevelInfoService.levelInfoHistory(customerUid);
        TableDataInfo dataTable = getDataTable(levelInfoVoList);
        Integer total = customerLevelInfoService.levelInfoHistoryTotal(customerUid);
        dataTable.setTotal(total);
        return dataTable;
    }

    /**
     * 查看客户账号
     */
    @ApiOperation(value = "查看客户账号")
    @PreAuthorize(hasPermi = "customer:Info:query")
    @GetMapping("/getCustomerAccount/{customerUid}")
    public TableDataInfo getCustomerAccount(@PathVariable Long customerUid) {
        List<CustomerInfoAccount> customerInfoAccountList = customerLevelInfoService.getCustomerAccount(customerUid);
        return getDataTable(customerInfoAccountList);
    }

    /**
     * 查看分享者小程序码
     */
    @ApiOperation(value = "查看分享者小程序码")
    @PreAuthorize(hasPermi = "customer:Info:query")
    @GetMapping("/getSharecustomerQrCode/{customerUid}")
    public AjaxResult getSharecustomerQrCode(@PathVariable Long customerUid) {
        String url = customerInfoService.getSharecustomerQrCode(customerUid);
        return AjaxResult.success("操作成功", url);
    }




    /**
     * 查询客户资金
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/getFundByUid/{customerUid}")
    public AjaxResult getFundByUid(@PathVariable Long customerUid) {
        return AjaxResult.success(customerFundService.getByCustomerId(customerUid));
    }

    /**
     * 查询客户各个门店的资金
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/listFundStore/{customerUid}")
    public TableDataInfo listFundStore(@PathVariable Long customerUid) {
        startPage();
        List<CustomerFundStoreVo> customerFundStoreVos = customerFundStoreService.listFundStore(customerUid);
        return getDataTable(customerFundStoreVos);
    }

    /**
     * 余额详情
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/myBalanceDetail/{customerUid}")
    public AjaxResult myBalanceDetail(@PathVariable Long customerUid,String businessType) {
        return customerInfoService.myBalanceDetail(customerUid,businessType);
    }

    /**
     * 查询客户消费记录
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/listBalanceChange/{customerUid}")
    public TableDataInfo listBalanceChange(@PathVariable Long customerUid) {
        startPage();
        List<CustomerFundStoreVo> customerFundStoreVos = customerFundStoreService.listFundStore(customerUid);
        return getDataTable(customerFundStoreVos);
    }

    /**
     * 查询用户的消费统计
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/getConsumptionStatics/{customerUid}")
    public AjaxResult getConsumptionStatics(@PathVariable Long customerUid) {
        return orderRechargeService.getConsumptionStatics(customerUid);
    }

    /**
     * 查询客户消费记录
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/listConsumptionByUid/{customerUid}")
    public TableDataInfo listConsumptionByUid(@PathVariable Long customerUid, @RequestParam(value = "time", required = false) String time) {
        startPage();
        List<CustomerRechargeVo> customerRechargeVos = orderRechargeService.listConsumptionByUid(customerUid, time);
        return getDataTable(customerRechargeVos);
    }


    /**
     * 查询客户档案
     *
     * @param customerUid
     * @return
     */
    @GetMapping("/selectListLimitByCustomerId/{customerUid}")
    public AjaxResult selectListLimitByCustomerId(@PathVariable Long customerUid) {
        return AjaxResult.success(customerInfoService.getRff(customerUid));
    }

    /**
     * 修改标签
     *
     * @param customerUid
     * @param list
     * @return
     */
    @PostMapping("/customerTags/addOrEdit/{customerUid}")
    public AjaxResult addOrEdit(@PathVariable Long customerUid, @RequestBody List<CustomerLabelInfo> list) {
        return AjaxResult.success(customerLabelInfoService.addOrEdit(customerUid, list));
    }
}
